﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class PlayerVehicle : MonoBehaviour {

	


	public float direction;
	public float acceleration;
	public float FuelBurn;
	public float distance;
	public float maxFuel;
	public float maxHull;
	public float speed;
	public float lifeTimer;
	public float lifeTime = 1.5f;
	public int vehiclePaint;
	
	public Slider controller;
	public Slider FuelSlider;
	public Slider HullSlider;
	public Slider distanceSlider;
	
	public Text cashText;
	
	public GameObject SmokeParticles;
	public GameObject DestroyParticles;
	GameObject Parts;
	
	public AudioSource Explosion;
	public AudioSource Impact;

	public PlayerStorage main;
	public MainService MainServ;
	
	public bool gamestarted;
	
	public CameraShake Shaker;
	
	public List<Material> PaintJobs;
	
	
	// Use this for initialization
	void OnEnable () 
	{
		main.LoadValues();
		CheckForVehicleType();
		PickPaint();
		MainService.GameStart += StartParticles;
		MainService.GameStart += StartPlayer;
		
		Shaker = GameObject.Find("Main Camera").GetComponent<CameraShake>();
		Explosion = GameObject.Find("Audio Source(Explosion)").GetComponent<AudioSource>();
		Impact = GameObject.Find("Audio Source(Impact)").GetComponent<AudioSource>();
		distance = main.distance;
		if (main.hull < 0)
		{
			main.hull = 10;
		}
		if (main.fuel < 0)
		{
			main.fuel = 10;
		}
		
		maxFuel = PlayerPrefs.GetFloat("MaxFuel");
		maxHull = PlayerPrefs.GetFloat("MaxHull");

		FuelSlider.maxValue = main.maxfuel;
		HullSlider.maxValue = main.maxhull;
		distanceSlider.maxValue = distance;
		distanceSlider.value = distance;
		FuelSlider.value = main.fuel;
		HullSlider.value = main.hull;
		cashText.text = "$" + main.cash.ToString();
	}
	
	void OnDisable()
	{
		MainService.GameStart -=StartParticles;
		MainService.GameStart -=StartPlayer;
	}
	
	void PickPaint()
	{

		Material PaintJob = PaintJobs[main.vehiclePaint];
		gameObject.GetComponentInChildren<MeshRenderer>().material = PaintJob;
	}
	
	void StartParticles()
	{
		SmokeParticles.SetActive(true);
	}


	void CheckForVehicleType ()
	{
		
		if (main.activeVehicle == 2)
		{
			main.distance = (main.distance *0.6f);
			main.hull = (main.hull * 0.6f);
		}
		
		if (main.activeVehicle == 1)
		{
			main.fuel = (main.fuel *1.3f);
			main.distance = (main.distance * 1.2f);
			
		}
		
		if (main.activeVehicle == 0)
		{
			main.hull = (main.hull *2f);
		}
	
	}
	
	
	void OnCollisionEnter(Collision col)
	{
		Impact.Play();
		Obstacle obj_hit = col.gameObject.GetComponent<Obstacle>();
		float damage = obj_hit.Damage;
		main.hull += -damage;
		Shaker.shakeIt ();
	}


	void addCash()
	{
		main.cash += 10;
	}
	
	
	void StartPlayer()
	{
		gamestarted = true;
		Shaker.shakeIt ();
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (gamestarted == true)
		{
			if(main.hull <= 0)
			{
				lifeTimer += 1.0F * Time.deltaTime;
				if (lifeTimer > lifeTime)
				{
					Explosion.Play();
					Destroy(gameObject);
					Parts = Instantiate (DestroyParticles, transform.position, Quaternion.identity) as GameObject;
				}
			}
			
			cashText.text = "$" + main.cash.ToString();
			distanceSlider.value = distance;
			FuelSlider.value = main.fuel;
			HullSlider.value = main.hull;
			direction =  - controller.value + 0.5f;
			
			if ( MainServ.gamePaused == false)
			{
				distance -= FuelBurn*15;
				main.fuel -= FuelBurn;
				transform.Translate(new Vector3( acceleration , 0,direction));
			}
			if ( MainServ.gamePaused == true)
			{
				transform.Translate(new Vector3( 0 , 0,0));
			
			}
		
		}
		
		
		
	}
}




